package com.yy.tech.data.logger.context;

import com.yy.tech.data.coder.encode.Encoder;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SecretRuleManager {
    private static SecretRuleManager manager = new SecretRuleManager();
    private Encoder encoder;
    private List<String> excludes;
    private List<String> properties;
    private  SecretRuleManager(){}

    public static SecretRuleManager getManager(){
        return manager;
    }

    public Encoder findEncoder(String name){
        ApplicationContext context = SpringContext.getContext();
        if (null != context) {
            return context.getBean(name, Encoder.class);
        }
        return null;
    }
    public Encoder findEncoder(){
        if (null == encoder){
            String value = getEnvironmentProperty(LoggerContent.KEY_DESENSITIZE_LOGGER_ENCODER);
            if (!StringUtils.isEmpty(value)){
                ApplicationContext context = SpringContext.getContext();
                if (null != context){
                    encoder=context.getBean(value,Encoder.class);
                    if (null == encoder){
                        try {
                            Class<Encoder> clz = (Class<Encoder>) Class.forName(value);
                            encoder =context.getBean(clz);
                        } catch (ClassNotFoundException e) {
                        }
                    }
                }
                if (null == encoder){
                    try{
                        Class<Encoder> clz = (Class<Encoder>) Class.forName(value);
                        encoder = clz.newInstance();
                    }catch (Exception e){

                    }
                }
            }
        }
        return encoder;
    }

    public boolean include(String name){
        if (CollectionUtils.isEmpty(excludes)){
            String value = getEnvironmentProperty(LoggerContent.KEY_DESENSITIZE_LOGGER_EXCLUDES);
            if (!StringUtils.isEmpty(value)){
                excludes = Stream.of(value.split(",")).collect(Collectors.toList());
            }
        }
        return null != excludes &&!excludes.contains(name);
    }

    public boolean match(String name){
        if (CollectionUtils.isEmpty(properties)){
            String value = getEnvironmentProperty(LoggerContent.KEY_DESENSITIZE_LOGGER_PROPERTIES);
            if (!StringUtils.isEmpty(value)){
                properties = Stream.of(value.split(",")).collect(Collectors.toList());
            }
        }
        return null != properties && properties.contains(name);
    }

    private String getEnvironmentProperty(String key){
        Environment environment = SpringContext.getEnvironment();
        if (null != environment){
            return environment.getProperty(key);
        }
        return null;
    }
}
